// index.js
import QRCode from '../../static/utils/QRCode.js'
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';

Page({
    //数据
    data:{
        saoma:true,
        year:0,
        month:0,
        day:0,
        hours:0,
        minutes:0,
        seconds:0,
        username:"",
        userInfo:{
            qrCode:0//添加默认值，不然会报错
        },//对象
        qrTxt:'绿码:健康码状态为低风险',
        qrColor:'#6caa72'
    },
    jumpHs(){
        //页面跳转
        wx.navigateTo({
          url: '/pages/hesuan/hesuan'
        })
    },
    //扫码函数
    saoma(){
        //false
        this.setData({
            saoma:false
        });
        //bug修复
        var that = this;
        wx.scanCode({
          onlyFromCamera: true,
          success:function(res){
                //把扫码的数据提交给后端的接口
                wx.request({
                    url: 'http://192.168.1.100/coordination/scanQrCode',
                    method:'POST',//请求方式
                    data:{//带两个
                        'name':res.result,
                        'userId':that.data.userInfo.id
                    },
                    header: {
                        'Content-Type': 'application/json;charset=UTF-8',
                    },
                    dataType: 'json',
                    success:(rsp) =>{
                        Dialog.alert({
                            title: '系统提示',
                            message: '成功',
                        }).then(() => {
                            //让码回来
                            that.setData({
                                saoma:true
                            });
                            //重新加载二维码
                            that.xrQR();
                        });
                    }
                })
          }
        })
    },
    //渲染二维码
    xrQR(color){
        let qrcode = new QRCode('canvas', {
            text: "我是绿码，我骗你的",
            width: 200,
            height: 200,
            colorDark: color,//前景色
            colorLight: "white",//后景色
            correctLevel : QRCode.CorrectLevel.H
        });
    },
    //渲染时间
    xrTime(){
        setInterval(() =>{
            var myDate = new Date();
            var year = myDate.getFullYear();    //获取完整的年份(4位,1970-????)
            var month = myDate.getMonth()+1;       //获取当前月份(0-11,0代表1月)
            var day = myDate.getDate();        //获取当前日(1-31)
            var hours = myDate.getHours();       //获取当前小时数(0-23)
            var minutes = myDate.getMinutes();     //获取当前分钟数(0-59)
            var seconds = myDate.getSeconds();     //获取当前秒数(0-59)
            if(seconds < 10){
                seconds = "0" + seconds;
            }
            this.setData({
                year:year,
                month:month,
                day:day,
                hours:hours,
                minutes:minutes,
                seconds:seconds
            })
        },1000);//1秒
    },
    //用于获取用户个人信息的函数
    getMyUserInfo(){
        //获取用户个人信息
        wx.getUserProfile({
            lang: 'zh_CN',
            desc:'用于获取二维码',
            success:(res) =>{
                var nickName = res.userInfo.nickName;

                setInterval(() =>{
                    wx.request({
                        url: 'http://192.168.1.112/coordination/getUserInfoByNickName?nickName='+nickName,
                        success:(suc) =>{
                            //把相应结果给数据源
                            this.setData({
                                //姓名
                                'userInfo.name':suc.data.data[0].name.replace(/(?<=.)./g, '*'),
                                //身份证号
                                'userInfo.idCard':suc.data.data[0].idCard.replace(/(?<=\d{3})\d{12}(?=\d{2})/,"************"),
                                //二维码
                                'userInfo.qrCode':suc.data.data[0].qrCode,
                                //用户的id
                                'userInfo.id':suc.data.data[0].id
                            })
                        }
                    })
                },5000)
            },
            fail:(err)=>{
                console.log(err);
            }
        })
    },
    qrChange(){
        setInterval(() =>{
            var code = this.data.userInfo.qrCode;
            if(code == 0){//绿码
                this.xrQR("#6caa72");
                this.setData({
                    qrTxt:'绿码:健康码状态为低风险',
                    qrColor:"#6caa72"
                })
            }else if(code == 1){//黄码
                this.xrQR("#fae573");
                this.setData({
                    qrTxt:'黄码:健康码状态为中风险',
                    qrColor:"#fae573"
                })
            }else if(code == 2){//红码
                this.xrQR("#fd2c2c");
                this.setData({
                    qrTxt:'红码:健康码状态为高风险',
                    qrColor:"#fd2c2c"
                })
            }else if(code == 3){//
                this.xrQR("#a734ef");
                this.setData({
                    qrTxt:'紫码:健康码状态为免疫',
                    qrColor:"#a734ef"
                })
            }
        },5000);
    },
    //页面加载时执行
    onLoad(){
        //this.xrQR();
        this.qrChange();
        this.xrTime();
        //弹框
        Dialog.alert({
            title: '系统提示',
            message: '我想要获取你的个人信息',
        }).then(() => {
            this.getMyUserInfo();
        });
    },
    getmsg(){
        console.log(this.data.username);
    }
})
